package ph.com.gs3.chat.fragments.view;

import java.util.ArrayList;
import java.util.List;

import ph.com.gs3.chat.R;
import ph.com.gs3.chat.adapters.ChatMessageListItemViewAdapter;
import ph.com.gs3.chat.model.valueobject.business.Message;
import ph.com.gs3.chat.model.valueobject.business.UserStub;
import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

public class ChatViewFragment extends Fragment {
	public static final String TAG = ChatViewFragment.class.getSimpleName();

	private ListView lvMessages;
	private EditText etMessage;
	private Button bSend;

	private ChatViewActionListener listener;

	private List<Message> messages;
	private UserStub currentUser;
	private UserStub recipientUser;
	private ChatMessageListItemViewAdapter adapter;

	public ChatViewFragment() {
		// TODO: move initialization of messages in the activity if the messages should be
		// stored
		messages = new ArrayList<>();
	}

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);

		try {
			listener = (ChatViewActionListener) activity;
		} catch (ClassCastException e) {
			throw new IllegalStateException(activity.getClass().getSimpleName()
			        + " must implement " + ChatViewActionListener.class.getSimpleName());
		}
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

		View rootView = inflater.inflate(R.layout.fragment_chat, container, false);

		adapter = new ChatMessageListItemViewAdapter(getActivity(), currentUser, messages);

		lvMessages = (ListView) rootView.findViewById(R.id.Chat_lvMessages);
		lvMessages.setAdapter(adapter);

		etMessage = (EditText) rootView.findViewById(R.id.Chat_etMessage);
		bSend = (Button) rootView.findViewById(R.id.Chat_bSend);

		bSend.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				String message = etMessage.getText().toString();
				listener.onSendMessageCommand(message);
				etMessage.setText("");

				addOwnMessage(message);

			}
		});

		return rootView;
	}

	public void addReceivedMessage(String messageString) {

		Message message = new Message();
		message.setAuthor(recipientUser);
		message.setMessage(messageString);

		messages.add(message);
		if (adapter != null) {
			adapter.notifyDataSetChanged();
			lvMessages.setSelection(messages.size() - 1);
		}

	}

	public void addOwnMessage(String messageString) {

		Message message = new Message();
		message.setAuthor(currentUser);
		message.setMessage(messageString);

		messages.add(message);
		adapter.notifyDataSetChanged();

		lvMessages.setSelection(messages.size() - 1);

	}

	public void setConversationParticipants(UserStub currentUser, UserStub recipientUser) {

		this.currentUser = currentUser;
		this.recipientUser = recipientUser;

	}

	public static interface ChatViewActionListener {
		public void onSendMessageCommand(String message);
	}

}
